import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 导入departments组织架构模块
import departments from '@/router/modules/departments'
// 导入employees员工管理模块
import employees from '@/router/modules/employees'
// 导入公司设置模块
import setting from '@/router/modules/setting'
// 导入权限设置模块
import permission from '@/router/modules/permission'
// 导入social社保模块
import social from '@/router/modules/social'
// 引入attendances考勤模块
import attendances from '@/router/modules/attendances'
// 导入salarys工资管理模块
import salarys from '@/router/modules/salarys'
// 导入approvals审批管理模块
import approvals from '@/router/modules/approvals'

// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    name: 'dashboard',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页面板', icon: 'dashboard' }
    }]
  }

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]
// 动态路由
export const asyncRoutes = [
  departments,
  employees,
  setting,
  permission,
  social,
  attendances,
  salarys,
  approvals
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  base: 'hr', // 添加路由字段配置
  scrollBehavior: () => ({ y: 0 }),
  // routes: [...constantRoutes, ...asyncRoutes] // 临时合并所有的路由
  // 这里不需要合并，开始只需要配置静态路由即可，动态路由需要根据用户的权限来追加
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
